package com.bos.dao.imp;

import com.bos.dao.IFuntionDao;
import com.bos.dao.base.imp.BaseDaoImp;
import com.bos.domain.Function;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Administrator
 */
@Repository
public class IFuntionDaoImp extends BaseDaoImp<Function> implements IFuntionDao {


    /**重写父类的findAll方法，由于hibernate设置了children不设置懒加载，如果一下子全部查出来就是现实很多的重复数据
     * 造成前台数据现实错误，因此只查顶级权限即可，他的子权限会全部被加载出来
     * */
    @Override
    public List<Function> findAll() {
        String hql = "from Function f where f.parentFunction is null";
        List<Function> funtions = (List<Function>) this.getHibernateTemplate().find(hql);
        return funtions;
    }

    @Override
    public List<Function> findByFuntionByUserId(String id) {
        /**利用hql进行面向对象查询，这中间不像sql一样体现出中间表,由于查出来的字段涉及到
        多个表，hibernate会把他们封装到一个对象数组里面，要想获得指定实体的数据就要指定查询之后封装的对象*/
        String hql = "select distinct f from Function  f left outer join  f.roles r left outer join r.users u where u.id= ?";
        List<Function> funtions = (List<Function>) this.getHibernateTemplate().find(hql,id);
        return funtions;
    }

    /**
     * 查询所有具有生成权限的菜单
     * @return*/
    @Override
    public List<Function> findAllMenu() {
        String hql = "select distinct f from Function f where  f.parentFunction is null  order by zindex asc ";
        List<Function> menus = (List<Function>) this.getHibernateTemplate().find(hql);
        return menus;
    }

    @Override
    public List<Function> findMenuByUserId(String uid) {
        String hql = "select distinct f from Function  f left outer join  f.roles r left outer join r.users u where u.id= ?" +
                "and f.parentFunction is null " +" and f.generatemenu = '1' order by f.zindex asc ";
        List<Function> menus = (List<Function>) this.getHibernateTemplate().find(hql,uid);
        return menus;
    }
}
